import { SoqlBuilder } from '../../src/salesforce/queryBuilder';
import Chai from 'chai';

const { expect } = Chai;

describe('Query builder', () => {
  [
    {
      record: {
        attributes: {
          type: 'Account',
        },
        Name: 'TestAccount',
      },
      soql: "SELECT Id, Name FROM Account WHERE Name = 'TestAccount'",
    },

    {
      record: {
        attributes: {
          type: 'Account',
        },
        Name: 'TestAccount',
        BoolField: true,
      },
      soql: "SELECT Id, Name, BoolField FROM Account WHERE Name = 'TestAccount' AND BoolField = true",
    },

    {
      record: {
        attributes: {
          type: 'Account',
        },
        NumberField: 5,
        BoolField: true,
      },
      soql: 'SELECT Id, NumberField, BoolField FROM Account WHERE NumberField = 5 AND BoolField = true',
    },
  ].forEach(({ record, soql }) =>
    it('transforms record to query', () => expect(SoqlBuilder.fromRecord(record).toSoql()).to.be.equal(soql))
  );

  it('can select additional fields if needed', () => {
    const record = {
      attributes: {
        type: 'Account',
      },
      Name: 'TestAccount',
    };
    const soql = SoqlBuilder.fromRecord(record).select(['otherfield', 'someotherfield']).toSoql();
    expect(soql).to.be.equal("SELECT Id, Name, otherfield, someotherfield FROM Account WHERE Name = 'TestAccount'");
  });

  it('can create query from list fo fields', () => {
    const soql = SoqlBuilder.select(['Name', 'Id'])
      .from('Contact')
      .where({
        Id: 'recId',
      })
      .toSoql();
    expect(soql).to.be.equal("SELECT Id, Name FROM Contact WHERE Id = 'recId'");
  });

  it('can use select all fields', () => {
    const soql = SoqlBuilder.selectAllFields()
      .from('Contact')
      .where({
        Id: 'recId',
      })
      .toSoql();
    expect(soql).to.be.equal("SELECT FIELDS(ALL) FROM Contact WHERE Id = 'recId' LIMIT 200");
  });

  it('throws error if no condition specified');
  it('can query all if asked explicitly');
  it('can create query by mongo-like templates');
});
